// src/snake-score/snake-score.controller.ts
import {
  Controller,
  Post,
  Body,
  Get,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { SnakeScoreService } from './snake-score.service';
import { Score } from './entities/score.entity';

@Controller('snake-score')
export class SnakeScoreController {
  constructor(private readonly snakeScoreService: SnakeScoreService) {}

  @Post()
  async addScore(
    @Body() scoreData: { username: string; score: number },
  ): Promise<Score> {
    const result = await this.snakeScoreService.addScore(
      scoreData.username,
      scoreData.score,
    );
    if (result === null) {
      throw new HttpException(
        'Score is not high enough to enter the top 10',
        HttpStatus.BAD_REQUEST,
      );
    }
    return result;
  }

  @Get('leaderboard')
  async getLeaderboard(): Promise<Score[]> {
    return this.snakeScoreService.getLeaderboard();
  }
}
